using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using System.Diagnostics;
using Microsoft.Xna.Framework.Graphics;
using S2Engine.Game;

namespace S2Engine.Text
{
    public class FontManager
    {
        /// <summary>
        /// Assumes fonts are in  single row in the order 01234567890$,
        /// </summary>

        public struct FontInfo
        {
            public string Filename;
            public string Characters;
            public int StartOffset;
            public int CharacterSpacing;
            public int CharacterWidth;
            public int CharacterHeight;
            public Texture2D Texture;

            public FontInfo(S2Game game, string fileName, string characters, int startOffset, int characterSpacing, int characterWidth, int characterHeight)
            {
                //Texture = Texture2D.FromFile(device, fileName);
                this.Texture = game.Content.Load<Texture2D>(fileName);
                Filename = fileName;
                Characters = characters;
                CharacterHeight = characterHeight;
                StartOffset = startOffset;
                CharacterSpacing = characterSpacing;
                CharacterWidth = characterWidth;
            }
        }

        public Dictionary<string, FontInfo> Fonts = new Dictionary<string, FontInfo>();
        private S2Game Game;
        public FontManager(S2Game game)
        {
            Game = game;
        }

        public void Dispose()
        {
            //if (batch != null)
            //{
            //    batch.Dispose();
            //    tch = null;
            //}
        }

  


        /// <summary>
        /// Draws some text from the given font
        /// </summary>
        /// <param name="game">Instance of the game we are drawing fonts for</param>
        /// <param name="fontStyle">Which font to use</param>
        /// <param name="x">X position in screen pixel space</param>
        /// <param name="y">Y position in screen pixel space</param>
        /// <param name="number">The number to draw</param>
        /// <param name="color">The color to draw it in</param>
        public void Draw(string font, int x, int y, int number, Vector4 color)
        {
            //No color - use 'white' i.e. use whatever is in the file
            Draw(font, x, y, number.ToString(), color);
        }

        /// <summary>
        /// Draws some text from the given font
        /// </summary>
        /// <param name="game">Instance of the game we are drawing fonts for</param>        
        /// <param name="fontStyle">Which font to use</param>
        /// <param name="x">X position in screen pixel space</param>
        /// <param name="y">Y position in screen pixel space</param>
        /// <param name="number">The number to draw</param>
        public void Draw( string font, int x, int y, int number)
        {
            //No color - use 'white' i.e. use whatever is in the file
            Draw(font, x, y, number.ToString(), new Vector4(1f, 1f, 1f, 1f));
        }

        /// <summary>
        /// Draws some text from the given font
        /// </summary>
        /// <param name="game">Instance of the game we are drawing fonts for</param>        
        /// <param name="fontStyle">Which font to use</param>
        /// <param name="x">X position in screen pixel space</param>
        /// <param name="y">Y position in screen pixel space</param>
        /// <param name="text">The text to draw</param>
        public void Draw( string font, int x, int y, string text)
        {
            //No color - use 'white' i.e. use whatever is in the file
            Draw(font, x, y, text, new Vector4(1f, 1f, 1f, 1f));
        }

        /// <summary>
        /// Draws some text from the given font
        /// </summary>
        /// <param name="game">Instance of the game we are drawing fonts for</param>        
        /// <param name="fontStyle">Which font to use</param>
        /// <param name="x">X position in screen pixel space</param>
        /// <param name="y">Y position in screen pixel space</param>
        /// <param name="text">The text to draw</param>
        public void Draw( string font, float x, float y, string text)
        {
            //No color - use 'white' i.e. use whatever is in the file
            Draw(font, x, y, text, new Vector4(1f, 1f, 1f, 1f));
        }



        /// <summary>
        /// Draws some text from the given font
        /// </summary>
        /// <param name="game">Instance of the game we are drawing fonts for</param>        
        /// <param name="fontStyle">Which font to use</param>
        /// <param name="x">X position in screen pixel space</param>
        /// <param name="y">Y position in screen pixel space</param>
        /// <param name="digits">The characters to draw</param>
        public void Draw(string font, int x, int y, string digits, float opacity)
        {
            //No color - use 'white' i.e. use whatever is in the file
            Draw(font, x, y, digits, new Vector4(1f, 1f, 1f, opacity));
        }

        /// <summary>
        /// Draws some text from the given font
        /// </summary>
        /// <param name="game">Instance of the game we are drawing fonts for</param>        
        /// <param name="fontStyle">Which font to use</param>
        /// <param name="x">X position in screen pixel space</param>
        /// <param name="y">Y position in screen pixel space</param>
        /// <param name="digits">The characters to draw</param>
        /// <param name="color">The color to draw it in</param>
        public void Draw(string font, float x, float y, string digits, Vector4 color)
        {
            float xPosition = x;
            FontInfo fontInfo = Fonts[font];

            for (int i = 0; i < digits.Length; i++)
            {
                //Do't draw anything if its a space character
                if (digits[i] != ' ')
                {
                    //Look up the character position
                    int character = fontInfo.Characters.IndexOf(digits[i]);

                    //IGraphicsDeviceService graphicsService = (IGraphicsDeviceService)game.GameServices.GetService(typeof(IGraphicsDeviceService));
                  //  Game.Batch.Begin(SpriteBlendMode.AlphaBlend);
                    //Draw the correct character at the correct position
                    char dig = digits[i];
                    int xxx = character * fontInfo.CharacterSpacing + fontInfo.StartOffset;
                    //if (character > 0)
                        xxx = character * (fontInfo.CharacterSpacing +fontInfo.CharacterWidth) + fontInfo.StartOffset ;

                    Game.Batch.Draw(fontInfo.Texture,
                        new Vector2(xPosition, y),
                        new Rectangle(xxx, 0, fontInfo.CharacterWidth, fontInfo.CharacterHeight),
                        new Color(color));

                   // Game.Batch.End();
                }

                //Move the position of the next character.
                //If the character is a comma or colon then use a 'fudge factor' to make the font look a little proportional
                xPosition += (digits[i] == ',' || digits[i] == ':') ? fontInfo.CharacterWidth / 2 : fontInfo.CharacterWidth;
            }

        }
    }
}
